﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="Global.asax.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using InnovaApp.Layers.CommonLayer.Controllers.ViewControllers.LayoutViewControllers;
using InnovaApp.Portals.MVC4Portal.App_Start;
using INNOVACALL.Framework.Common.Logging;

namespace InnovaApp.Portals.MVC4Portal
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    /// <summary>
    /// Class MvcApplication
    /// </summary>
    public class MvcApplication : System.Web.HttpApplication
    {

        /// <summary>
        /// Application_s the start.
        /// </summary>
        protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();

            //Warning : Call order is important
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            Logger logger = new Logger();

        }

        /// <summary>
        /// Application_s the begin request.
        /// </summary>
        protected void Application_BeginRequest()
        {
        }
        /// <summary>
        /// Application_s the end request.
        /// </summary>
        protected void Application_EndRequest()
        {
            if (
                Context.Response.StatusCode >= 400
                )
            {
                string[] tLanguages = ConfigurationManager.AppSettings["Languages"].Split(',');
                string[] tUserLanguages = HttpContext.Current.Request.UserLanguages;
                string[] tUserLanguagesAllowed = tLanguages.Intersect(tUserLanguages.Select(x => x.ToLower())).ToArray();
                string language = InnovaApp.Layers.CommonLayer.Controllers.Environment.DefaultLanguage;

                if (tUserLanguagesAllowed.Length > 0)
                {
                    language = tUserLanguages[0];
                }
                language = language.ToLower();
                Exception exception = Server.GetLastError();
                if (exception == null || !(typeof(INNOVACALL.Framework.Common.InnovacallExceptions.I_InnovacallExceptions_Base).IsAssignableFrom(exception.GetType())))
                {
                    Logger.Info(enLog.Technical, string.Format("r&d {0} Context.Response.StatusCode : {110}", Context.Request.RawUrl, Context.Response.StatusCode));
                } else {
                    ((INNOVACALL.Framework.Common.InnovacallExceptions.I_InnovacallExceptions_Base)exception).LogIt();
                }

                Server.ClearError();
                Response.Clear();
                var routeData = new RouteData();

                routeData.Values["friendlyUrl"] = language + "/Error/" + Context.Response.StatusCode;
                routeData.Values["controller"] = "Error";
                routeData.Values["action"] = "Index";
                routeData.Values["errorCode"] = Context.Response.StatusCode.ToString();
                routeData.Values["language"] = language;
                routeData.Values["exception"] = exception;

                IController c = new ErrorController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
            }
        }
        /// <summary>
        /// Session_s the start.
        /// </summary>
        protected void Session_Start()
        {
            Logger.Debug(enLog.Business, string.Format(@"Session started with Id : {0}", Session.SessionID));
        }
    }
}